package com.smfx.yxt.controller.student;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smfx.yxt.common.AppUser;
import com.smfx.yxt.common.Const;
import com.smfx.yxt.common.MenuConst;
import com.smfx.yxt.controller.BaseController;
import com.smfx.yxt.model.StudentInfo;
import com.smfx.yxt.reqVo.KccsCourseQueryVO;
import com.smfx.yxt.service.IKccsCourseInfoService;
import com.smfx.yxt.service.IStudentService;
import com.smfx.yxt.view.KccsCourseStudentSignView;

@RequestMapping(Const.STUDENT_ROLE + MenuConst.INTEREST_COURSE)
@Controller
public class ChooseCourseControoller extends BaseController{
	
	private Logger logger = LoggerFactory.getLogger(ChooseCourseControoller.class);
	
	@Autowired
	private IKccsCourseInfoService kccsCourseInfoService;
	@Autowired
	private IStudentService studentService;
	
	@RequestMapping(value={"","/"})
	public String index(Model model) {
		AppUser appUser = getAppUser();
		KccsCourseQueryVO vo = new KccsCourseQueryVO();
		vo.setTermId(getCurrentTerm().getId());
		vo.setParentId(appUser.getParent().getId());
		List<KccsCourseStudentSignView> list = kccsCourseInfoService.selectSignListForParent(vo);
		model.addAttribute("list", list);
		return "kccsCourse/studentIndex";
	}
	
	@RequestMapping("list")
	public String list(Model model) {
		return "kccsCourse/studentList";
	}
	
	@RequestMapping("list/query")
	@ResponseBody Object listQuery(KccsCourseQueryVO vo) {
		AppUser appUser = getAppUser();
		StudentInfo studentInfo = studentService.selectStudentBindParent(vo.getStudentId(), appUser.getOpenid());
		if(studentInfo == null) {
			return null;
		}
		vo.setStudyType(getAppUser().getStudyType());
		vo.setClazz(studentInfo.getClazz());
		vo.setGrade(studentInfo.getGrade());
		vo.setTermId(getCurrentTerm().getId());
		vo.setFinishYear(getCurrentTerm().getFinishYear());
		vo.setKey(vo.getCourseId() == null ? null : String.valueOf(vo.getCourseId()));
		return null;
	}
	
	@RequestMapping("detail/{courseId}")
	public String detail(Model model, @PathVariable Integer courseId) {
		//学生ID
		String xbsid = request.getParameter("xbsid");
		//已报名的课程id
		String xboid = request.getParameter("xboid");
		if(StringUtils.isNotBlank(xboid)) {
			KccsCourseQueryVO queryVO = new KccsCourseQueryVO();
			queryVO.setTermId(getCurrentTerm().getId());
			queryVO.setStudentId(Integer.valueOf(xbsid));
			queryVO.setCourseId(Integer.valueOf(xboid));
			KccsCourseStudentSignView signView = kccsCourseInfoService.selectSignedCourseForStudent(queryVO);		
			if(signView == null) return null;
			model.addAttribute("signView", signView);
		}
		model.addAttribute("detail", kccsCourseInfoService.selectViewById(courseId));
		return "kccsCourse/studentDetail";
	}
	
}
